import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, List, Detail, Brands, SourceOrder, HistoryList, HistoryDetail} from './DTO';
const baseUrl = '/afterSales/api/v1';
import {baseDataStatus} from '../Enum';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '索赔结算单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '预结算{resourceName}'
}, {resourceName});


const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});


const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '提交{resourceName}'
}, {resourceName});

const invoiceDesc = formatMessage({
    id: 'api.message.invoiceDesc',
    defaultMessage: '开票通知'
});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const resettleDesc = formatMessage({
    id: 'api.message.resettleDesc',
    defaultMessage: '反结算'
});
const confirmPIDesc = formatMessage({
    id: 'api.message.confirmPIDesc',
    defaultMessage: '确认PI'
});
const financeDesc = formatMessage({
    id: 'api.message.financeDesc',
    defaultMessage: '上报财务'
});
const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});

const exportPiDesc = formatMessage({
    id: 'api.exportPiDesc',
    defaultMessage: '导出PI'
});


const passDesc = formatMessage({
    id: 'api.message.passDesc',
    defaultMessage: '审核通过'
});

const rejectDesc = formatMessage({
    id: 'api.message.rejectDesc',
    defaultMessage: '审核不通过'
});

const sourceOrderDesc = formatMessage({
    id: 'api.message.sourceOrderDesc',
    defaultMessage: '查询待结算单据'
});

const getHistoryListDesc = formatMessage({
    id: 'api.getHistoryListDesc',
    defaultMessage: '查询索赔结算管理历史'
});

const getHistoryDetailDesc = formatMessage({
    id: 'api.getHistoryDetailDesc',
    defaultMessage: '查询索赔结算管理历史详情'
});
const uppiInfoDesc = formatMessage({
    id: 'api.uppiInfoDesc',
    defaultMessage: '上传PI'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/claimSettlement/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, initQuery));

export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brands)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, initQuery));
};
// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claimSettlements/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 新增
export const addInfo = data => request(`${baseUrl}/claimSettlements`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 查询详情
export const getDetail = id => request(`${baseUrl}/claimSettlements/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 提交
export const submitInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}/submit`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));

// 开票通知
export const invoiceInfo = data => request(`${baseUrl}/claimSettlements/invoice`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, invoiceDesc));


// 作废
export const abandonInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

// 反结算
export const reSettleInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}/reSettlement`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, resettleDesc));
// 确认PI
export const confirmPIInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}/confirmPI`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmPIDesc));

// 上报财务
export const financeInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}/finance`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, financeDesc));


// 审核通过
export const passInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}/pass`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, passDesc));

// 审核不通过
export const rejectInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}/reject`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectDesc));

// 导出
export const exportInfo = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claimSettlements/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};

// 台账导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/claimSettlements/ledgerExport/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};


// 查询待结算的单据
export const getSourceOrders = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claimSettlementSourceOrders?${queryString}`, null, SourceOrder)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, sourceOrderDesc));
};

//索赔结算历史查询
export const getHistoryList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/claimSettlementHistories/forCurrentUser?${queryString}`, null, HistoryList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getHistoryListDesc));
};

//索赔结算历史查询详情
export const getHistoryDetail = id => request(`${baseUrl}/claimSettlementHistories/${id}`, null, HistoryDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getHistoryDetailDesc));

// 上传PI
export const uppiInfo = (id, data) => request(`${baseUrl}/claimSettlements/${id}/uppi`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, uppiInfoDesc));

// 导出
export const exportPi = id => request(`${baseUrl}/claimSettlements/${id}/exportpi`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportPiDesc));

//历史附件下载
export const getHistoryDownload = data => {
    const queryString = stringify(data);
    return request(`${baseUrl}/files/ftpIsDownload?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, null));
};

// 多级审核通过
export const passApprove = (id, data) => request(`${baseUrl}/claimSettlements/${id}/approve/pass`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, passDesc));

// 多级审核不通过
export const rejectApprove = (id, data) => request(`${baseUrl}/claimSettlements/${id}/approve/reject`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectDesc));

// 查询待结算详情
export const getSourceOrderDetail = id => request(`${baseUrl}/claimSettlementSourceOrders/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, sourceOrderDesc));

// 查询去重详情
export const getDetailRepeat = id => request(`${baseUrl}/claimSettlements/${id}/repeat`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));
